﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using KeanuMvc.Reference;
using System.Collections;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;
using System.Net;
using System.ServiceModel.Channels;


namespace HIS.Controllers {

    public abstract class AppBaseController {

        #region Fields
        private KeanuMvc.Reference.ClientRestService clientRestService = null;
        public string ClientComet = string.Empty;
        public IDictionary Data = new Hashtable();
        private static ITemplateEngine fileTemplateEngine = null;
        private static ITemplateEngine memoryTemplateEngine = null;
        #endregion

        #region Properties
        public KeanuMvc.Reference.ClientRestService RestService {
            get {
                return clientRestService;
            }
        }
        #endregion

        #region Constructors
        public AppBaseController() {
            clientRestService = new KeanuMvc.Reference.ClientRestService();
            clientRestService.HeaderOptional.Add("his-apikey", ConfigurationManager.AppSettings["apikey"]);
            clientRestService.HeaderOptional.Add("his-hospitalcode", ConfigurationManager.AppSettings["hospitalcode"]);
            

            if(HttpContext.Current.Session["userticketdata"] != null)
                clientRestService.HeaderOptional.Add("his-membershipuserid", HIS.Utils.Helper.GetUserId());

            Data.Add("headerinformation", ConfigurationManager.AppSettings["hospitalinitial"]);
            Data.Add("rooturl", ConfigurationManager.AppSettings["rooturl"].ToString());
            Data.Add("hucode", ConfigurationManager.AppSettings["hospitalcode"].ToString());
            SetSideBarMenu();
        }

        #endregion

        public abstract void Index();

        #region Methods

        public void GoToUrl(string UrlPath) {
            HttpContext.Current.Response.Redirect(UrlPath);
        }

        private void SetSideBarMenu() {
            if (HttpContext.Current.Session["navigation"] != null) {
                Data.Add("UserNav", HttpContext.Current.Session["navigation"].ToString());
                return;
            }

            try {
                string encryptedTicket = KeanuMvc.Reference.CryptoString.Decrypt(HttpContext.Current.Session["userticketdata"].ToString());
                string userRole = encryptedTicket.Split('|')[1];

                string strUri = Global.ServiceUriDictionary["navgroupsrolebyid"] + userRole + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = RestService.GetJson(strUri);

                HISModels.Entities.SysNavigationGroup sysNavigationGroup = JsonConvert.DeserializeObject<SysNavigationGroup>(stringReader.ReadToEnd());

                HttpContext.Current.Session["navigation"] = HIS.Utils.Helper.GenerateNavigationForUser(RestService, sysNavigationGroup.Id);

                Data.Add("UserNav", HttpContext.Current.Session["navigation"].ToString());
            } catch {
            }
            
        }

        public T GetRequest<T>(string url) {
            return default(T);
        }

        #region Render Methods

        private string RenderFileProcess(IDictionary HasTableData, string TemplateName) {
            if (fileTemplateEngine == null) fileTemplateEngine = TemplateEngineFactory.CreateTemplateFileEngine(string.Format(@"{0}\views", HttpContext.Current.Request.PhysicalApplicationPath), true);
            IDictionary temData = HasTableData != null ? HasTableData : Data;
            string strReturn = fileTemplateEngine.Process(temData, TemplateName);
            return strReturn;
        }

        public void RenderFile(string TemplateName) {
            HttpContext.Current.Response.Write(RenderFileProcess(null, TemplateName));
        }

        public void RenderFile(IDictionary HasTableData, string TemplateName) {
            HttpContext.Current.Response.Write(RenderFileProcess(HasTableData, TemplateName));
        }

        private string RenderStringProcess(IDictionary HasTableData, string TemplateName) {
            if (memoryTemplateEngine == null) memoryTemplateEngine = TemplateEngineFactory.CreateTemplateMemoryEngine(true);

            IDictionary temData = HasTableData != null ? HasTableData : Data;
            string strReturn = memoryTemplateEngine.Process(temData, TemplateName);
            return strReturn;
        }

        public void RenderString(string TemplateName) {
            HttpContext.Current.Response.Write(RenderStringProcess(null, TemplateName));
        }

        public void RenderString(IDictionary HasTableData, string TemplateName) {
            HttpContext.Current.Response.Write(RenderStringProcess(HasTableData, TemplateName));
        }

        public string RenderFileToString(string TemplateName) {
            return RenderFileProcess(null, TemplateName);
        }

        public string RenderFileToString(IDictionary HasTableData, string TemplateName) {
            return RenderFileProcess(HasTableData, TemplateName);
        }

        public string RenderStringToString(string TemplateName) {
            return RenderStringProcess(null, TemplateName);
        }

        public string RenderStringToString(IDictionary HasTableData, string TemplateName) {
            return RenderStringProcess(HasTableData, TemplateName);
        }


        #endregion
        #endregion

    }
}